package com.nexus.plugin.exception;

import cn.hutool.core.exceptions.ValidateException;
import com.nexus.core.exception.AuthenicException;
import com.nexus.core.exception.ServiceException;
import com.nexus.core.exception.SystemException;
import com.nexus.core.exception.UnauthorException;
import com.nexus.core.response.ResponseMessage;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Date 2025/7/31 15:25
 * @Author luzhengning
 **/
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 业务异常
     *
     * @author luzhengning
     * @date 2023/3/17 17:18
     */
    @ExceptionHandler(ServiceException.class)
    public ResponseEntity<?> handleServiceException(ServiceException e, HttpServletRequest request) {
        e.printStackTrace();
        return ResponseMessage.ERROR_CODE(e.getErrorCode(), e.getMessage());
    }

    /**
     * 认证异常
     *
     * @author luzhengning
     * @date 2023/3/17 17:28
     */
    @ExceptionHandler(AuthenicException.class)
    public ResponseEntity<?> handleAuthenicException(AuthenicException e, HttpServletRequest request) {
        e.printStackTrace();
        return ResponseMessage.Unauthorized(e.getMessage());
    }

    /**
     * 未经授权的异常
     *
     * @author luzhengning
     * @date 2023/3/17 17:29
     */
    @ExceptionHandler(UnauthorException.class)
    public ResponseEntity<?> handleUnauthoriException(UnauthorException e, HttpServletRequest request) {
        e.printStackTrace();
        return ResponseMessage.Unauthorized(e.getMessage());
    }

    /**
     * 系统异常
     *
     * @author luzhengning
     * @date 2023/3/20 13:26
     */
    @ExceptionHandler(SystemException.class)
    public ResponseEntity<?> handleUnauthoriException(SystemException e, HttpServletRequest request) {
        e.printStackTrace();
        return ResponseMessage.ERROR_CODE(e.getErrorCode(), e.getMessage());
    }

    /**
     * 自定义校验异常
     *
     * @author luzhengning
     * @date 2023/3/20 13:27
     */
    @ExceptionHandler(ValidateException.class)
    public ResponseEntity<?> handleUnauthoriException(ValidateException e, HttpServletRequest request) {
        e.printStackTrace();
        return ResponseMessage.BAD_REQUEST(null, e.getMessage());
    }

    /**
     * 请求体异常
     *
     * @author luzhengning
     * @date 2023/3/17 17:27
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResponseEntity<?> handleHttpMessageNotReadableException(HttpMessageNotReadableException e, HttpServletRequest request) {
        e.printStackTrace();
        return ResponseMessage.BAD_REQUEST("", "请求体异常");
    }

    /**
     * 数据校验异常
     *
     * @author luzhengning
     * @date 2023/3/17 17:27
     */
    @ExceptionHandler(BindException.class)
    public ResponseEntity<?> handleBindException(BindException e) {
        String message = e.getAllErrors().get(0).getDefaultMessage();
        e.printStackTrace();
        return ResponseMessage.BAD_REQUEST(null, message);
    }

    // @ExceptionHandler(org.springframework.web.HttpRequestMethodNotSupportedException.class)
    // public ResponseEntity<?> handleHttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException e) {
    //     e.printStackTrace();
    //     return ResponseMessage.body(HttpStatus.METHOD_NOT_ALLOWED,e.getMessage());
    // }


    /**
     * TOKEN过期
     *
     * @author luzhengning
     * @date 2023/3/23 11:49
     */
    @ExceptionHandler(com.auth0.jwt.exceptions.TokenExpiredException.class)
    public ResponseEntity<?> handleHttpRequestMethodNotSupportedException(com.auth0.jwt.exceptions.TokenExpiredException e) {
        e.printStackTrace();
        return ResponseMessage.body(HttpStatus.UNAUTHORIZED, "登录凭证过期，请重新登录");
    }

    /**
     * 全局异常
     *
     * @author luzhengning
     * @date 2023/3/17 17:27
     */
    @ExceptionHandler(Exception.class)
    public ResponseEntity<?> handleApiUnauthorizedExecption(Exception e, HttpServletRequest request) {
        e.printStackTrace();
        return ResponseMessage.ERROR(e.getMessage(), "服务器内部错误");
    }

}